import { towerModel } from '@/types/model'
import { AbstractDraw } from './AbstractDraw'

export class DrawTower extends AbstractDraw {
  constructor(protected ctx: CanvasRenderingContext2D, startY: number, private towers: towerModel[]) {
    super(ctx, startY)
  }

  public draw() {
    for (const tower of this.towers) {
      this.drawTowerXY(tower)
      this.drawTowerText(tower)
    }
  }

  private drawTowerXY(tower: towerModel) {
    const tStyle = tower.tStyle
    const [x, y] = JSON.parse(tower.xy!)
    switch (tStyle) {
      case '耐张':
        this.drawNaiZhangTower([x, y])
        break
      case '直线':
        this.drawZhiXianTower([x, y])
        break
      case '张力场':
        this.drawZhangLiChang([x, y])
        break
      case '牵引场':
        this.drawQianYingChang([x, y])
        break
      default:
        break
    }
  }

  drawTowerText = (tower: towerModel) => {
    const [x, y] = JSON.parse(tower.xy!)
    // 绘制名称
    super.drawText(tower.tName, super.scaleXY(x + 30, y + 15), 'black', 'left', 'middle')
    super.drawText(Math.round(tower.altitude) + 'm', super.scaleXY(x + 30, y), '#8CABD9', 'left', 'middle')
  }

  private drawNaiZhangTower([x, y]: [number, number]) {
    // 绘制竖线
    super.drawline(super.scaleXY(x, y + 10), super.scaleXY(x, y - 20))
    // 绘制横线
    super.drawline(super.scaleXY(x - 10, y - 5), super.scaleXY(x, y))
    // 绘制横线
    super.drawline(super.scaleXY(x, y), super.scaleXY(x + 10, y - 5))
  }

  private drawZhiXianTower([x, y]: [number, number]) {
    super.drawline(super.scaleXY(x, y + 10), super.scaleXY(x, y - 20))
    super.drawline(super.scaleXY(x - 10, y), super.scaleXY(x + 10, y))
  }

  private drawZhangLiChang([x, y]: [number, number]) {
    super.drawFillCircle(super.scaleXY(x, y), 4, 'red')
    super.drawFillCircle(super.scaleXY(x + 6, y), 4, 'red')
    super.drawCircle(super.scaleXY(x, y), 6, 'blue')
    super.drawCircle(super.scaleXY(x + 6, y), 6, 'blue')
  }

  private drawQianYingChang([x, y]: [number, number]) {
    super.drawFillCircle(super.scaleXY(x, y), 4, 'red')
    super.drawCircle(super.scaleXY(x, y), 6, 'blue')
  }
}
